package com.cyufeng.blog.service.mapper;

import com.cyufeng.blog.common.SpringUtil;
import com.cyufeng.blog.dto.ArticleDto;
import com.cyufeng.blog.entity.Article;
import com.cyufeng.blog.entity.Sort;
import com.cyufeng.blog.repository.SortRepository;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

import java.sql.Date;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 文章
 *
 * @author Cyufeng
 * Data:2019.01.07
 */
@Component
public class ArticleMapper {
    ApplicationContext appCtx = SpringUtil.getApplicationContext();
    SortRepository sortRepository = appCtx.getBean(SortRepository.class);

//    //得到所有的类别数据
//    List<Sort> sortList = sortRepository.getSort();
//    // list转map   key：ID   value:name
//    Map<Integer, String> sortMaps = sortList.stream().collect(Collectors.toMap(Sort::getId, Sort::getName));

    public ArticleDto entityTodto(Article article) {
        //得到所有的类别数据
        List<Sort> sortList = sortRepository.getSort();
        // list转map   key：ID   value:name
        Map<Integer, String> sortMaps = sortList.stream().collect(Collectors.toMap(Sort::getId, Sort::getName));
        ArticleDto articleDto = new ArticleDto();

        articleDto.setId(article.getId());
        articleDto.setCode(article.getCode());
        articleDto.setTitle(article.getTitle());
        articleDto.setSortId(article.getSort());
        articleDto.setSortName(sortMaps.get(article.getSort()));//通过key：Id找到 value：name
//        articleDto.setSort(article.getSort());
        articleDto.setAuthor(article.getAuthor());
        articleDto.setTel(article.getTel());
        articleDto.setDetail(article.getDetail());
        articleDto.setEditorial(article.getEditorial());
        articleDto.setIsEnable(article.getIsEnable());
        articleDto.setIsTool(article.getIsTool());
        articleDto.setDr(article.getDr());
//        将详细时间转化为简单时间
//        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
//        ParsePosition pos = new ParsePosition(0);
//        Date toMadetile = formatter.parse(article.getMadeTime().toString(), pos);
        articleDto.setMadeTime(article.getMadeTime());

        articleDto.setDelTime(article.getDelTime());
        articleDto.setUpTime(article.getUpTime());

        return articleDto;
    }

    public Article dtoToentity(ArticleDto articleDto) {
        Article article = new Article();

        article.setId(articleDto.getId());
        article.setCode(articleDto.getCode());
        article.setTitle(articleDto.getTitle());
        article.setSort(articleDto.getSortId());
        article.setAuthor(articleDto.getAuthor());
        article.setTel(articleDto.getTel());
        article.setDetail(articleDto.getDetail());
        article.setEditorial(articleDto.getEditorial());
        article.setIsEnable(articleDto.getIsEnable());
        article.setIsTool(articleDto.getIsTool());
        article.setDr(articleDto.getDr());
        article.setMadeTime(articleDto.getMadeTime());
        article.setDelTime(articleDto.getDelTime());
        article.setUpTime(articleDto.getUpTime());
        return article;
    }

    //list集合
    public List<ArticleDto> entityTodto(List<Article> articleList) {
        //得到所有的类别数据
        List<Sort> sortList = sortRepository.getSort();
        // list转map   key：ID   value:name
        Map<Integer, String> sortMaps = sortList.stream().collect(Collectors.toMap(Sort::getId, Sort::getName));
        List<ArticleDto> articleDtoList = new ArrayList<>();
        for (int i = 0; i < articleList.size(); i++) {
            Article article = articleList.get(i);
            ArticleDto articleDto = new ArticleDto();

            articleDto.setId(article.getId());
            articleDto.setCode(article.getCode());
            articleDto.setTitle(article.getTitle());

            articleDto.setSortId(article.getSort());
            articleDto.setSortName(sortMaps.get(article.getSort()));//通过key：Id找到 value：name//通过key：Id找到 value：name
            articleDto.setAuthor(article.getAuthor());
            articleDto.setTel(article.getTel());
            articleDto.setDetail(article.getDetail());
            articleDto.setEditorial(article.getEditorial());
            articleDto.setIsEnable(article.getIsEnable());
            articleDto.setIsTool(article.getIsTool());
            articleDto.setDr(article.getDr());

            //        将详细时间转化为简单时间
//            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
//            ParsePosition pos = new ParsePosition(0);
//            Date toMadetile = formatter.parse(article.getMadeTime().toString(), pos);
            articleDto.setMadeTime(article.getMadeTime());
            articleDto.setDelTime(article.getDelTime());
            articleDto.setUpTime(article.getUpTime());

            articleDtoList.add(articleDto);
        }

        return articleDtoList;
    }

    public List<Article> dtoToentity(List<ArticleDto> articleDtoList) {
        List<Article> articleList = new ArrayList<>();
        for (int i = 0; i < articleList.size(); i++) {
            Article article = new Article();
            ArticleDto articleDto = articleDtoList.get(i);

            article.setId(articleDto.getId());
            article.setCode(articleDto.getCode());
            article.setTitle(articleDto.getTitle());
            article.setSort(articleDto.getSortId());
            article.setAuthor(articleDto.getAuthor());
            article.setTel(articleDto.getTel());
            article.setDetail(articleDto.getDetail());
            article.setEditorial(articleDto.getEditorial());
            article.setIsEnable(articleDto.getIsEnable());
            article.setIsTool(articleDto.getIsTool());
            article.setDr(articleDto.getDr());
            article.setMadeTime(articleDto.getMadeTime());
            article.setDelTime(articleDto.getDelTime());
            article.setUpTime(articleDto.getUpTime());

            articleList.add(article);
        }
        return articleList;
    }
}
